#pragma once
#include <d3d9.h>
#include <d3dx9.h>
#include <vector>
#include <string>
#include <fstream>
#include "terrain.h"
#include "heightmap.h"
#include "grass.h"
#include "../utility/defs.h"
#include "../utility/Gets.h"
#include "../utility/camera.h"
#include "../shaderMgr.h"
#include "../textureMgr.h"
#include "../heightmapMgr.h"
using namespace std;

const int TileRows = 16;
const int TileCols = 16;
const int ChunkRows = 17;
const int ChunkCols = 17;
const int GridRows = TileRows*(ChunkRows-1) + 1;
const int GridCols = TileCols*(ChunkCols-1) + 1;
const float DW = 150.0f;
const float DD = 150.0f;
const float ChunkWidth = DW*(ChunkCols-1);
const float ChunkDepth = DD*(ChunkRows-1);
const float TileWidth = ChunkWidth*TileCols;
const float TileDepth = ChunkDepth*TileRows;
const D3DXVECTOR3 Vec3Center = D3DXVECTOR3(0.0f,0.0f,0.0f);

class cTerrainBuilder
{
public:
	cTerrainBuilder();
	void ReadHeightFiles(string filename);
	void BuildTerrain(string grasslocation,string treelocation);
	void OnInit(string grasslocation,string treelocation);
	void Render();
	void Update(float dt);
	void Release();
	void SetTextureScale(float scale){ m_texScale = scale; }
	float GetHeight( float x, float z );
	vector<cTerrainBase*>& GetVisibleTerrainTile(){
		return mapVisibleTerrainTile;
	}
private:
	void SetTexture(WCHAR* srcfiles);
	void SetShader(WCHAR* srcfiels);
	void GetLocation(TerrainVertex* pVertex,string location,char** inFile);
private:
	cShaderMgr* m_pShaderMgr;
	cTextureMgr* m_pTextureMgr;
	cHeightmap* m_pHeightMap;

	float m_texScale;
	D3DXVECTOR3 m_LightPos;

	vector<cTerrainBase*> mapTerrainTile;
	vector<cTerrainBase*> mapVisibleTerrainTile;
	LPDIRECT3DDEVICE9 m_pDevice9;
};